package cn.itsource.hrm.asserts;

import cn.itsource.hrm.exception.GlobleException;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GlobleAssert {

    //手机号的正则表达式
    public static final String REGEX_PHONE = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(16[5,6])|(17[0-8])|(18[0-9])|(19[1、5、8、9]))\\d{8}$";
    //邮箱正则表达式
    public static final String REGEX_EMAIL = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";

    /**
     * 断言字符串不为空，如果为空，抛异常
     */
    public static void hasText(String text, String message) {
        if (!StringUtils.hasText(text)) {
            throw new GlobleException(message);
        }
    }

    /**
     * 断言字手机号
     */
    public static void isMobile(String mobile, String message) {
        Pattern p = Pattern.compile(REGEX_PHONE, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(mobile);
        if(!m.matches()){
            throw new GlobleException(message);
        }
    }
    /**
     * 断言邮箱
     */
    public static void isEmail(String email, String message) {
        Pattern p = Pattern.compile(REGEX_EMAIL, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(email);
        if(!m.matches()){
            throw new GlobleException(message);
        }
    }

}
